package com.herohead.notifications;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.json.simple.JSONObject;

import com.herohead.Misc;

public class Instapush {


	String api_key;
	String api_secret;
	String api_message;
	Boolean useProxy = false;
	String proxyAddress = "";
	String proxyPort = "";

	private final static String USER_AGENT = "Mozilla/5.0";

	public void checkForProxy() {

		String s = Misc.getLocalHostIPaddress();
		if (!s.contains("192.168.50")) {
			setProxy("proxy.am.mt.mtnet", "8080");
		}
	}

	/**
	 * @return - proxy Address
	 */
	public String getProxyAddress() {
		return proxyAddress;
	}

	/**
	 * @return - proxy port
	 */
	public String getProxyPort() {
		return proxyPort;
	}

	@SuppressWarnings("unchecked")
	public String sendMessage (String message) throws Exception {

		api_message = message;

		//if (api_key == null) return "Key not set";
		//if (api_secret == null) return "Secret not set";

		//add reuqest header
		/**
	 String url = "https://api.instapush.im/v1/post";
		URL obj = new URL(url);
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", USER_AGENT);
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
		con.setRequestProperty("x-instapush-appid", "537ddc93a4c48aa055f98314");
		con.setRequestProperty("x-instapush-appsecret", "9b788007f58c93308793f5a687ad6c0e");
		con.setRequestProperty("Content-Type","application/json");
		 **/

		//curl -X POST -H "x-instapush-appid: 537e8617a4c48a0504f98314" -H "x-instapush-appsecret: 7cfcbd54ac806b5c8ffeb6ff66eb9710" -H "Content-Type: application/json" -d '{"event":"servertest","trackers":{"server":"myserver"}}' https://api.instapush.im/v1/post


		String url = "https://api.instapush.im/v1/post";
		URL obj = new URL(url);
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", USER_AGENT);
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

		//con.setRequestProperty("x-instapush-token","537ddc86a4c48a9955f98314");
		con.setRequestProperty("X-INSTAPUSH-APPID", "537e8617a4c48a0504f98314-12");
		con.setRequestProperty("X-INSTAPUSH-APPSECRET","7cfcbd54ac806b5c8ffeb6ff66eb9710");
		//con.setRequestProperty("Content-Type","application/json");
		con.setRequestProperty("event","servertest");
		con.setRequestProperty("trackers","{\"server\":\"myserver\"}");
		System.out.println(con.getRequestProperty("trackers"));
		//String u = "-H \"x-instapush-appid: 537e8617a4c48a0504f98314\"";
		//String u1 = "-H \"x-instapush-appsecret: 9b788007f58c93308793f5a687ad6c0e\"";
		//String u2 = "-H \"Content-Type: application/json\"";
		//String u3 = "-d '{\"server\":\"MYAPI\",\"trackers\":[\"myBigServer\",\"myStatus\"],\"message\":\"{server} has signed up from {status}\"'";

		//String u = "\"x-instapush-appid: 537ddc93a4c48aa055f98314\"";
		//String u1 = "\"x-instapush-appsecret: 9b788007f58c93308793f5a687ad6c0e\"";
		//String u2 = "\"Content-Type: application/json\"";
		JSONObject jsobj = new JSONObject();
		jsobj.put("event", "servertest");
		jsobj.put("trackers", "server");

		System.out.println(jsobj.toJSONString());




		String u3 = "'{\"event\":\"servertest\",\"trackers\":{\"server\":\"myserver\"}}'";


		System.out.println(u3);

		String urlParameters = u3;
		//String urlParameters = jsobj.toJSONString();

		// Send post request
		con.setDoOutput(true);
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());



		wr.writeBytes(urlParameters);

		wr.flush();
		wr.close();

		int responseCode = con.getResponseCode();
		System.out.println(con.getResponseMessage());

		System.out.println("\nSending 'POST' request to URL : " + url);
		System.out.println("Post parameters : " + urlParameters);
		System.out.println("Response Code : " + responseCode);

		BufferedReader in = new BufferedReader(
				new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
		System.out.println(con.getRequestProperty("msg"));
		//print result
		return response.toString();

	}

	public void setKey (String s_api_key) {
		api_key = s_api_key;
	}

	/**
	 * @param proxy - set proxy server
	 * @param port - set proxy port
	 */
	public void setProxy(String proxy, String port) {

		proxyAddress = proxy;
		proxyPort = port;

		System.setProperty("https.proxyHost", proxyAddress);
		System.setProperty("https.proxyPort", proxyPort);
		useproxy(true);
	}


	/**
	 * @param s_api_secret - secret key for the API key
	 */
	public void setSecret (String s_api_secret) {
		api_secret = s_api_secret;
	}


	/**
	 * @param useproxy - set proxy
	 */
	public void useproxy(Boolean useproxy) {
		useProxy = useproxy;
	}
}
